package com.utc.fjc.command.changeowningproject;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JTabbedPane;

import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentProject;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.ui.common.RACUIUtil;
import com.teamcenter.rac.util.combobox.iComboBox;
import com.utc.teamcenter.common.app.base.BaseBeanDialog;
import com.utc.teamcenter.common.app.dialog.bean.BeanICombobox;
import com.utc.teamcenter.common.util.QueryUtil;
import com.utc.teamcenter.common.util.TCUtil;

public class ChangeOwningProjectDialog extends BaseBeanDialog {

	private static final long serialVersionUID = 1L;
	private BeanICombobox projectList = null;
	private InterfaceAIFComponent[] targets;
//	private List<TCComponentProject> projects = new ArrayList<TCComponentProject>();
	private  TCComponent[] allproject = null;
	private  final TCSession session = RACUIUtil.getTCSession();
	private  TCComponentUser tcuser = session.getUser();
	private  TCComponentProject tcpro = null;
	private  List<String> projects = new ArrayList<String>();
	private  List<String> listUser = new ArrayList<String>();
	private  String userPro="";

	public ChangeOwningProjectDialog(InterfaceAIFComponent[] targets) {
		super();
		this.targets = targets;
//		this.projects = getAllProject();
//		TCComponentProject project = TCUtil.getTCSession().getCurrentProject();
		try {
			allproject = tcuser.getReferenceListProperty("user_projects");
			for (int i = 0; i < allproject.length; i++) {
				projects.add(allproject[i].toString());
			}
			tcpro = session.getCurrentProject();
			if(tcpro!=null){
				Vector teamUser = session.getCurrentProject().getTeam();
				TCComponentUser[] users = (TCComponentUser[]) teamUser.get(2);
				for (int i = 0; i < users.length; i++) {
					listUser.add(users[i].getStringProperty("user_name"));
				}
				userPro =tcpro.toString();
			}else{
				userPro="";
			}
			
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	/*
	private List<TCComponentProject> getAllProject() {
		try {
			String[] entries = new String[] { Messages.ChangeOwningProjectDialog_1 };
			String[] values = new String[] { "*" }; //$NON-NLS-1$
			TCComponent[] projectComs = QueryUtil.query(
					Messages.ChangeOwningProjectDialog_0, entries, values);
			for (TCComponent com : projectComs) {
				if (com instanceof TCComponentProject) {
					TCComponentProject project = (TCComponentProject) com;
					projects.add(project);
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
		return projects;
	}
	*/

	@Override
	public void initalBeanComponents() {
		this.projectList = new BeanICombobox(
				"project", Messages.ChangeOwningProjectDialog_3, userPro, projects, false); //$NON-NLS-1$  //$NON-NLS-3$
		super.addBeanComponent(this.projectList);
		
		((iComboBox) projectList.getComponent()).addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					String selectName = projectList.getText();
					for (int i = 0; i < allproject.length; i++) {
						if (selectName.equals(allproject[i].getStringProperty("object_string"))) {
							tcpro = (TCComponentProject)allproject[i];
						}
					}
					
				} catch (TCException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
	}

	@Override
	public AbstractAIFOperation getOperation() {
//		TCComponentProject selectedProj = (TCComponentProject) projectList
//				.getSelectedItem();
		return new ChangeOwningProjectOperation(targets, tcpro);
	}

	@Override
	public String getTitle() {
		return Messages.ChangeOwningProjectDialog_2;
	}

	@Override
	public void clear(Container container, JTabbedPane panel) {
		this.projectList.clear();
	}

}
